What is is-subdir?
The is-subdir npm package is a utility that helps determine if a given directory is a subdirectory of another directory. This can be useful in various scenarios such as validating file paths, ensuring security by restricting access to certain directories, and more.
What are is-subdir's main functionalities?
Check if a directory is a subdirectory
This feature allows you to check if a given directory is a subdirectory of another directory. In this example, '/home/user/projects/my-app' is a subdirectory of '/home/user/projects', so the function returns true.
const isSubdir = require('is-subdir');
const parentDir = '/home/user/projects';
const subDir = '/home/user/projects/my-app';
console.log(isSubdir(parentDir, subDir)); // true
Check if a directory is not a subdirectory
This feature allows you to check if a given directory is not a subdirectory of another directory. In this example, '/home/user/other-projects' is not a subdirectory of '/home/user/projects', so the function returns false.
const isSubdir = require('is-subdir');
const parentDir = '/home/user/projects';
const notSubDir = '/home/user/other-projects';
console.log(isSubdir(parentDir, notSubDir)); // false
Other packages similar to is-subdir
path-is-inside
The path-is-inside package provides similar functionality by checking if one path is inside another path. It is useful for validating file paths and ensuring that a given path is within a certain directory. Compared to is-subdir, path-is-inside offers a more general approach to path validation.
is-path-inside
The is-path-inside package checks if a path is inside another path. It is similar to is-subdir but focuses on paths rather than directories specifically. This package is useful for ensuring that a file or directory is within a certain path, providing a broader use case compared to is-subdir.
is-subdir
Return whether a directory is a subdirectory of another directory
Cross-platform. Works correctly on Windows, where directory paths can start with disk drive letters in different casings. Like c:\foo
and C:\foo\bar
.
Returns true
when the directories match. The isSubdir.strict()
variant only returns true if the second parameter is a strict subdir of the first and not the same.
Installation
<npm|yarn|pnpm> add is-subdir
Usage
'use strict'
const path = require('path')
const isSubdir = require('is-subdir')
console.log(isSubdir(process.cwd(), path.resolve('node_modules')))
console.log(isSubdir.strict('node_modules/tape', '../tape'))
API
isSubdir(parentDir, subdir): boolean
isSubdir.strict(parentDir, subdir): boolean
License
MIT © Zoltan Kochan